package flc.ast.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.DanceDetailActivity;
import flc.ast.activity.MusicActivity;
import flc.ast.adapter.ChoiceAdapter;
import flc.ast.adapter.HotAdapter;
import flc.ast.adapter.MusicAdapter;
import flc.ast.databinding.FragmentMusicBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link MusicFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class MusicFragment extends BaseNoModelFragment<FragmentMusicBinding> {

    private HotAdapter hotAdapter;
    private MusicAdapter musicAdapter;
    private ChoiceAdapter choiceAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_music;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.rvHotList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        hotAdapter = new HotAdapter();
        mDataBinding.rvHotList.setAdapter(hotAdapter);
        hotAdapter.setOnItemClickListener(this);

        mDataBinding.rvMusicList.setLayoutManager(new LinearLayoutManager(mContext));
        musicAdapter = new MusicAdapter();
        mDataBinding.rvMusicList.setAdapter(musicAdapter);
        musicAdapter.setOnItemClickListener(this);

        mDataBinding.rvChoiceList.setLayoutManager(new GridLayoutManager(mContext, 2));
        choiceAdapter = new ChoiceAdapter();
        mDataBinding.rvChoiceList.setAdapter(choiceAdapter);
        choiceAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean = (StkResBean) adapter.getItem(position);
        if (adapter == musicAdapter) {
            MusicActivity.stkResBean = bean;
            startActivity(MusicActivity.class);
        } else {
            DanceDetailActivity.stkResBean = bean;
            startActivity(DanceDetailActivity.class);
        }
    }

    @Override
    protected void initData() {
        getHotData();
        getMusicData();
        getChoiceData();
    }

    private void getHotData() {
        StkResApi.getTagResourceList(this, AppConfig.HOT_DATA, StkResApi.createParamMap(0, 5), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        hotAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getMusicData() {
        StkResApi.getTagResourceList(this, AppConfig.MUSIC_DATA, StkResApi.createParamMap(0, 3), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        musicAdapter.setList(result);
                    }
                }
            }
        });
    }

    private void getChoiceData() {
        StkResApi.getTagResourceList(this, AppConfig.CHOICE_DATA, StkResApi.createParamMap(0, 8), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        choiceAdapter.setList(result);
                    }
                }
            }
        });
    }

}